package com.ys.nms.module.business.controller.admin.localCompanyDevicetType;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import com.ys.nms.framework.excel.core.util.ExcelUtils;
import com.ys.nms.module.business.controller.admin.localCompanyDevicetType.vo.CompanyDeviceTypeListReqVO;
import com.ys.nms.module.business.controller.admin.localCompanyDevicetType.vo.CompanyDeviceTypeRespVO;
import com.ys.nms.module.business.controller.admin.localCompanyDevicetType.vo.CompanyDeviceTypeSaveReqVO;
import com.ys.nms.module.business.dal.dataobject.localCompanyDevicetType.CompanyDeviceTypeDO;
import com.ys.nms.module.business.service.localCompanyDevicetType.CompanyDeviceTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.ys.nms.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 设备类型")
@RestController
@RequestMapping("/business/company-device-type")
@Validated
public class CompanyDeviceTypeController {

    @Resource
    private CompanyDeviceTypeService companyDeviceTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建设备类型")
    @PreAuthorize("@ss.hasPermission('nms:company-device-type:create')")
    public CommonResult<Long> createCompanyDeviceType(@Valid @RequestBody CompanyDeviceTypeSaveReqVO createReqVO) {
        return success(companyDeviceTypeService.createCompanyDeviceType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备类型")
    @PreAuthorize("@ss.hasPermission('nms:company-device-type:update')")
    public CommonResult<Boolean> updateCompanyDeviceType(@Valid @RequestBody CompanyDeviceTypeSaveReqVO updateReqVO) {
        companyDeviceTypeService.updateCompanyDeviceType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nms:company-device-type:delete')")
    public CommonResult<Boolean> deleteCompanyDeviceType(@RequestParam("id") Long id) {
        companyDeviceTypeService.deleteCompanyDeviceType(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得设备类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nms:company-device-type:query')")
    public CommonResult<CompanyDeviceTypeRespVO> getCompanyDeviceType(@RequestParam("id") Long id) {
     CompanyDeviceTypeDO companyDeviceType = companyDeviceTypeService.getCompanyDeviceType(id);
        return success(BeanUtils.toBean(companyDeviceType, CompanyDeviceTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备类型列表")
    @PreAuthorize("@ss.hasPermission('nms:company-device-type:query')")
    public CommonResult<List<CompanyDeviceTypeRespVO>> getCompanyDeviceTypeList(@Valid CompanyDeviceTypeListReqVO listReqVO) {
        List<CompanyDeviceTypeDO> list = companyDeviceTypeService.getCompanyDeviceTypeList(listReqVO);
        return success(BeanUtils.toBean(list, CompanyDeviceTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备类型 Excel")
    @PreAuthorize("@ss.hasPermission('nms:company-device-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCompanyDeviceTypeExcel(@Valid CompanyDeviceTypeListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<CompanyDeviceTypeDO> list = companyDeviceTypeService.getCompanyDeviceTypeList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "设备类型.xls", "数据", CompanyDeviceTypeRespVO.class,
                        BeanUtils.toBean(list, CompanyDeviceTypeRespVO.class));
    }

}